// 引入动态路由列表
import { asyncRoutes, constantRoutes } from '@/router'
const state = {
  // 完整的菜单=静态路由 + 筛选后的静态路由
  routes: []
}

const mutations = {
  setRoutes(state, data) {
    //   静态路由和筛选后的动态路由拼接
    state.routes = [
      ...constantRoutes,
      ...data
    ]
  }
}

const actions = {
  // 过滤动态路由
  filterRoutes(store, menus) {
    const routes = asyncRoutes.filter(route => {
      return menus.includes(route.name)
    })

    console.log('过滤后的路由:', routes)
    // 准备恢复菜单
    store.commit('setRoutes', routes)
    // 这里是actions被dispatch调用, 这里的返回值可以直接在被调用时获取
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

